package icu.bughub.app.app.ui.components

import MainFrame
import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.google.accompanist.insets.ProvideWindowInsets
import icu.bughub.app.app.compositionLocal.LocalUserViewModel
import icu.bughub.app.app.ui.navigation.Destinations
import icu.bughub.app.app.ui.screens.ArticleDetailScreen
import icu.bughub.app.app.ui.screens.LoginScreen
import icu.bughub.app.app.ui.screens.VideoDetailScreen
import icu.bughub.app.app.viewmodel.UserViewModel


/**
 * 导航控制器
 */
@SuppressLint("ViewModelConstructorInComposable")
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun NavHostApp() {

    val navController = rememberNavController()

    ProvideWindowInsets {

        CompositionLocalProvider(LocalUserViewModel provides UserViewModel(LocalContext.current)) {

            val userViewModel = LocalUserViewModel.current

            NavHost(
                navController = navController,
                startDestination = Destinations.HomeFrame.route
            ) {
                composable(route = Destinations.HomeFrame.route) {
                    MainFrame(onNavigateToArticleDetail = {
                        Log.i("NavHostApp", "onNavigateToArticleDetail")
                        //跳转文章详情页
                        navController.navigate(Destinations.ArticleDetail.route)
                    }, onNavigateToVideoDetail = {
                        //跳转视频详情页
                        navController.navigate(Destinations.VideoDetail.route)
                    }, onNavigateToStudyHistory = {
                        navController.navigate(Destinations.Login.route)
                    })

                }

                //首页大框架
                /*   composable(Destinations.HomeFrame.route) {
                       MainFrame(onNavigateToArticleDetail = {
                           Log.i("NavHostApp", "onNavigateToArticleDetail")
                           navController.navigate(Destinations.ArticleDetail.route)
                       })
                   }*/

                //文章详情页
                composable(Destinations.ArticleDetail.route) {
                    ArticleDetailScreen(onBack = {
                        //返回
                        navController.popBackStack()
                    })
                }


                //视频详情页
                composable(Destinations.VideoDetail.route) {
                    VideoDetailScreen(onBack = {
                        //返回
                        navController.popBackStack()
                    })
                }

                composable(Destinations.Login.route) {
                    if (userViewModel.logged) {
                        // 已登录
                        MainFrame(onNavigateToArticleDetail = {
                            Log.i("NavHostApp", "onNavigateToArticleDetail")
                            //跳转文章详情页
                            navController.navigate(Destinations.ArticleDetail.route)
                        }, onNavigateToVideoDetail = {
                            //跳转视频详情页
                            navController.navigate(Destinations.VideoDetail.route)
                        }, onNavigateToStudyHistory = {
                            navController.navigate(Destinations.Login.route)
                        })

                    } else {
                        // 未登录
                        LoginScreen(back = {
                            navController.popBackStack()
                        })
                    }

                }

                /*composable(route = Destinations.ArticleDetail.route) {
                    AnimatedVisibility(visible = true) {
                        ArticleDetailScreen()
                    }
                }*/
            }

            /*NavHost(
                navController = navController,
                startDestination = Destinations.HomeFrame.route,
            ) {
                //首页大框架
                composable(Destinations.HomeFrame.route) {
                    MainFrame(onNavigateToArticleDetail = {
                        navController.navigate(Destinations.ArticleDetailScreen.route)
                    })
                }

                //文章详情页
                composable(Destinations.ArticleDetailScreen.route) {
                    ArticleDetailScreen()
                }
            }*/

        }
    }
}
